package com.belf.console.service.impl;

import com.belf.common.core.domain.AjaxResult;
import com.belf.console.domain.DataBatchConf;
import com.belf.console.mapper.DataBatchConfMapper;
import com.belf.console.service.IDataBatchConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 批量同步配置Service业务层处理
 * 
 * @author belf
 * @date 2024-08-20
 */
@Service
public class DataBatchConfServiceImpl implements IDataBatchConfService
{
    @Autowired
    private DataBatchConfMapper dataBatchConfMapper;

    /**
     * 查询批量同步配置
     * 
     * @param id 批量同步配置主键
     * @return 批量同步配置
     */
    @Override
    public DataBatchConf selectDataBatchConfById(Long id)
    {
        return dataBatchConfMapper.selectDataBatchConfById(id);
    }

    @Override
    public List<DataBatchConf> selectDataBatchConfByTaskId(Long id) {
        List<DataBatchConf> dataBatchConfs = dataBatchConfMapper.selectDataBatchConfByTaskId(id);
        for (DataBatchConf batchConf : dataBatchConfs) {
            if (batchConf.getInsertColumnstr() != null) {
                String[] split = batchConf.getInsertColumnstr().split(",");
                batchConf.setInsertColumns(Arrays.stream(split).toList());
            }

        }
        /*if (dataBatchConfs != null && dataBatchConfs.size()>0) {
            for (DataBatchConf dataBatchConf : dataBatchConfs) {
                String columnSyncConfMap = dataBatchConf.getColumnSyncConfMap();
                if (columnSyncConfMap != null) {
                    List<ColumnSyncConf> columnSyncConfs = JSONArray.parseArray(columnSyncConfMap, ColumnSyncConf.class);
                    dataBatchConf.setColumns(columnSyncConfs);
                }
            }
        }*/
        return dataBatchConfs;
    }

    /**
     * 查询批量同步配置列表z
     * 
     * @param dataBatchConf 批量同步配置
     * @return 批量同步配置
     */
    @Override
    public List<DataBatchConf> selectDataBatchConfList(DataBatchConf dataBatchConf)
    {
        List<DataBatchConf> dataBatchConfs = dataBatchConfMapper.selectDataBatchConfList(dataBatchConf);
        for (DataBatchConf batchConf : dataBatchConfs) {
            if (batchConf.getInsertColumnstr() != null) {
                String[] split = batchConf.getInsertColumnstr().split(",");
                batchConf.setInsertColumns(Arrays.stream(split).toList());
            }

        }
        return dataBatchConfs;
    }

    /**
     * 新增批量同步配置
     * 
     * @param dataBatchConf 批量同步配置
     * @return 结果
     */
    @Override
    public int insertDataBatchConf(DataBatchConf dataBatchConf)
    {
        if (dataBatchConf.getInsertColumns() != null && dataBatchConf.getInsertColumns().size()>0) {
            dataBatchConf.setInsertColumnstr(String.join(",", dataBatchConf.getInsertColumns()));
        }
        return dataBatchConfMapper.insertDataBatchConf(dataBatchConf);
    }

    /**
     * 修改批量同步配置
     * 
     * @param dataBatchConf 批量同步配置
     * @return 结果
     */
    @Override
    public int updateDataBatchConf(DataBatchConf dataBatchConf)
    {
       /* if (dataBatchConf.getColumns() != null && dataBatchConf.getColumns().size()>0) {
            String jsonString = JSONArray.toJSONString(dataBatchConf.getColumns());
            dataBatchConf.setColumnSyncConfMap(jsonString);
        }*/

        if (dataBatchConf.getInsertColumns() != null && dataBatchConf.getInsertColumns().size()>0) {
            dataBatchConf.setInsertColumnstr(String.join(",", dataBatchConf.getInsertColumns()));
        }

        return dataBatchConfMapper.updateDataBatchConf(dataBatchConf);
    }

    /**
     * 批量删除批量同步配置
     * 
     * @param ids 需要删除的批量同步配置主键
     * @return 结果
     */
    @Override
    public int deleteDataBatchConfByIds(Long[] ids)
    {
        return dataBatchConfMapper.deleteDataBatchConfByIds(ids);
    }

    /**
     * 删除批量同步配置信息
     * 
     * @param id 批量同步配置主键
     * @return 结果
     */
    @Override
    public int deleteDataBatchConfById(Long id)
    {
        return dataBatchConfMapper.deleteDataBatchConfById(id);
    }

    @Override
    public int deleteDataBatchConfByTaskId(Long taskId)
    {
        return dataBatchConfMapper.deleteDataBatchConfByTaskId(taskId);
    }

    @Override
    public int deleteDataBatchConfByTaskIds(Long[] ids) {
        return dataBatchConfMapper.deleteDataBatchConfByTaskIds(ids);
    }

    @Override
    public AjaxResult pushLast(DataBatchConf dataBatchConf) {
        updateDataBatchConf(dataBatchConf);
        return AjaxResult.success();
    }
}
